/*
 * @(#)TransferToClient.java    Jul 23, 2010
 *
 * Copyright 2009 Nyber(Shanghai Itd. All rights reserved.
 */

package com.lab.net;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.nio.channels.FileChannel;
import java.nio.channels.SocketChannel;

/**
 * Class description goes here.
 *
 * @author nickevin@gmail.com
 * @version Jul 23, 2010 10:10:31 AM
 */

public class TransferToClient {

	String host = "localhost";
	int port = 9026;

	public static void main(String[] args) throws IOException {
		TransferToClient client = new TransferToClient();
		client.send();
	}

	public void send() {
		FileChannel fileChannel = null;
		try {
			SocketAddress server = new InetSocketAddress(host, port);
			SocketChannel socketChannel = SocketChannel.open();
			socketChannel.connect(server);
			socketChannel.configureBlocking(false);

			File file = new File("D:\\Programs\\eclipse-jee-galileo-win32.zip");
			FileInputStream is = new FileInputStream(file);
			long fsize = file.length();

			fileChannel = is.getChannel();

			long start = System.currentTimeMillis();
			long curnset = 0;
			curnset = fileChannel.transferTo(0, fsize, socketChannel);
			System.out.println("total bytes transferred--" + curnset
					+ " and time taken in MS--"
					+ (System.currentTimeMillis() - start));
		} catch (Exception e) {
			e.printStackTrace();
			if (fileChannel != null) {
				try {
					fileChannel.close();
				} catch (IOException e1) {
					e1.printStackTrace();
				}
			}
		}
	}

}
